package com.ebupt.migu.music.autoTri.Service.Impl;

import com.ebupt.migu.music.autoTri.Mapper.VipBlockMapper;
import com.ebupt.migu.music.autoTri.Service.VipBlockService;
import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @description: 自动触发-会员红包策略分组管理
 * @author: liuyangyang@ebupt.com
 * @date: 2021/6/23 14:22
 */
@Slf4j
@Service
public class VipBlockServiceImpl implements VipBlockService {

    @Autowired
    private VipBlockMapper vipBlockMapper;

    /**
     * 新增自动触发-会员红包策略分组
     *
     * @param params
     */
    @Override
    public ResultObject add(Map<String, Object> params) {
        ResultObject res = new ResultObject();
        try {
            String blockName = String.valueOf(params.get("blockName"));
            if (vipBlockMapper.isBlockNameExist(blockName)) {
                res.setCode(StatusEnum.VIP_BLOCK_NAME_EXIST.getCode());
                res.setMsg(StatusEnum.VIP_BLOCK_NAME_EXIST.getMsg());
                return res;
            }
            vipBlockMapper.add(params);
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg("创建成功");
            log.info("{}创建了自动触发策略分组：{}", params.get("creatorName"), blockName);
            return res;
        } catch (Exception e) {
            log.error("创建自动触发策略分组失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

    /**
     * 查询自动触发-会员红包策略分组
     *
     * @param map
     * @return
     */
    @Override
    public ResultObject query(Map<String, Object> map) {
        ResultObject<Page<Map<String, Object>>> res = new ResultObject<>();
        try {
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            int total = vipBlockMapper.totalBlocks(map);
            List<Map<String, Object>> list = vipBlockMapper.query(map);
            // 分页
            Page<Map<String, Object>> page = new Page<>();
            page.setTotal(total);
            page.setData(list);
            res.setData(page);
            return res;
        } catch (Exception e) {
            log.error("查询自动触发策略分组失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

}
